package com.wy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wy.dto.GoodsAuctionDto;
import com.wy.dto.GoodsInfoDto;
import com.wy.entity.GoodsAuction;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author 王渝
 */
@Mapper
public interface GoodsAuctionMapper extends BaseMapper<GoodsAuction> {

    /**
     * 批量更新拍卖生成订单状态 为2
     */
    @Update("<script>" +
            "update goods_auction " +
            "         set ordering = 2" +
            "        WHERE goods_id in" +
            "        <foreach collection ='goodsIds' item='id' separator =',' open = '(' close = ')' >" +
            "           #{id}" +
            "        </foreach >" +
            "</script>" )
    int updateBatch(List<Long> goodsIds);

    /**
     * 根据商品id查询商品的出价次数和当前价格
     * @param goodsId 商品id
     * @return
     */
    @Select("SELECT max(bid_amount) max_amount,count(*) count FROM goods_auction WHERE goods_id = #{goodsId}")
    GoodsInfoDto queryCountAndMax(Long goodsId);

    /**
     * 根据用户id查询参与拍卖的信息（已经结束）
     * @param userId 用户id
     * @return
     */
    @Select("SELECT * FROM (\n" +
            "(SELECT goods_info.goods_name,\n" +
            "goods_info.goods_id,goods_info.starting_time,\n" +
            "goods_info.end_time,goods_auction.bid_amount,\n" +
            "goods_type.type_name\n" +
            "FROM goods_info,goods_auction,goods_type\n" +
            "WHERE goods_info.goods_id = goods_auction.goods_id \n" +
            "AND goods_auction.user_id = #{userId}\n" +
            "and goods_info.type_id = goods_type.type_id ) table1\n" +
            ",\n" +
            "(SELECT goods_id, max(bid_amount) max_amount\n" +
            "FROM goods_auction\n" +
            "GROUP BY goods_id) table2\n" +
            ")\n" +
            "WHERE table1.goods_id = table2.goods_id\n" +
            "and  now() >= end_time")
    List<GoodsAuctionDto> queryInfoByUserIdIsEnd(Long userId);


    /**
     * 根据用户id查询参与拍卖的信息（未结束）
     * @param userId 用户id
     * @return
     */
    @Select("SELECT * FROM (\n" +
            "(SELECT goods_info.goods_name,\n" +
            "goods_info.goods_id,goods_info.starting_time,\n" +
            "goods_info.end_time,goods_auction.bid_amount,\n" +
            "goods_type.type_name\n" +
            "FROM goods_info,goods_auction,goods_type\n" +
            "WHERE goods_info.goods_id = goods_auction.goods_id \n" +
            "AND goods_auction.user_id = #{userId}\n" +
            "and goods_info.type_id = goods_type.type_id ) table1\n" +
            ",\n" +
            "(SELECT goods_id, max(bid_amount) max_amount\n" +
            "FROM goods_auction\n" +
            "GROUP BY goods_id) table2\n" +
            ")\n" +
            "WHERE table1.goods_id = table2.goods_id\n" +
            "and  now() < end_time")
    List<GoodsAuctionDto> queryInfoByUserIdIng(Long userId);

    /**
     * 查询商品的出价记录
     * @param goodsId 商品id
     * @return
     */
    @Select("SELECT auction_id,bid_amount,update_time,user_name\n" +
            "FROM (SELECT user_name,user_id FROM user_info) table1,goods_auction\n" +
            "WHERE table1.user_id = goods_auction.user_id\n" +
            "and goods_id = #{goodsId}\n")
    List<GoodsAuction> queryListByGoodsId(Long goodsId);
}